@extends('layouts.app')
@section('content')
<script src="{{env('URL_SUB', '')}}/static/datetimepicker/bootstrap-datetimepicker.js"></script>
<script src="{{env('URL_SUB', '')}}/static/datetimepicker/bootstrap-datetimepicker.fr.js"></script>
<link href="{{env('URL_SUB', '')}}/static/datetimepicker/bootstrap-datetimepicker.min.css" rel="stylesheet">
    <section class="content-header">
        <h1>
            登山
            <small>排名列表</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="/dashboard"><i class="fa fa-dashboard"></i>家</a></li>
            <li><a href="#">登山</a></li>
            <li class="active">排名列表</li>
        </ol>

    </section>

    <section class="content-header">

        @if (Session::has('mgssages'))
            <div class="alert alert-success alert-dismissible">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <h4><i class="icon fa fa-check"></i> 状态!</h4>
                <ul>
                    @foreach (Session::get('mgssages') as $message)
                        <li>{{ $message }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
        <ul class="nav nav-tabs" id="myTab">
            <li><a href="{{env('URL_SUB', '')}}/admin/hillclimb/ranking1">单次最远距离(6.16-6.30)</a></li>
            <li><a href="{{env('URL_SUB', '')}}/admin/hillclimb/ranking2">参与游戏总次数(7.01-7.15)</a></li>
            <li class="active"><a href="{{env('URL_SUB', '')}}/admin/hillclimb/ranking3">参与游戏总距离(7.16-7.31)</a></li>
        </ul>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">搜索</h3>
                        <div class="box-tools"></div>
                    </div>
                    <div class="box-body">
                        <div id="example2_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">
                            <form class="form-inline" role="form" method="get" id="frmSearch">
                                <div class="form-group">
                                    <input type="text" name="nickname" class="form-control input-sm" style="width:9em" placeholder="请输入昵称">
                                </div>&nbsp;
                                <div class="form-group">
                                    <input type="text" name="openid" class="form-control input-sm" style="width:9em" placeholder="请输入openid">
                                </div>&nbsp;
                                <div class="form-group">
                                    <input type="text" name="capital_name" class="form-control input-sm" style="width:9em" placeholder="请输入姓名">
                                </div>&nbsp;
                                <div class="form-group">
                                    <input type="text" name="capital_phone" class="form-control input-sm" style="width:9em" placeholder="请输入手机号">
                                </div>&nbsp;
                                <div class="form-group">
                                    <label class="sr-only" for="exampleInputEmail3"></label>
                                    <input type="text" class="form-control pull-right datepicker" name="time" data-date-format="yyyy-mm-dd" placeholder="请选择日期">
                                </div>
                                &nbsp;
                                <div class="form-group">
                                    <select class="form-control type form-inline" name="ishided">
                                        <option value="">是否拉黑</option>
                                        <option value="1">已拉黑</option>
                                        <option value="0">未拉黑</option>
                                    </select>
                                </div>&nbsp;
                                <input type="submit" class="btn  btn-success" value="搜索">&nbsp;
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <script>
            $('.datepicker').datetimepicker({
                autoclose: true,
                format: 'yyyy-mm-dd',
                minView : 2
            });
            $(function() {
                var search = location.search.substring(1);
                if (search) {
                    try {
                        var query = JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');
                        $.each(query, function(k, v) {
                            $('[name=' + k + ']').val(v);
                        });
                    } catch (e) {
                        console.log(e);
                    }
                }
            })
        </script>
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">排名列表</h3>
                        <div class="box-tools">
                            <span class="download-show"></span>
                            <a data-href="{{env('URL_SUB', '')}}/admin/hillclimb/export?period=2&ishided={{$ishided}}&time={{$time}}&nickname={{$nickname}}&openid={{$openid}}&capital_name={{$capital_name}}&capital_phone={{$capital_phone}}" class="btn btn-primary btn-export">导出</a>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body">
                        <div id="example2_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">
                            <div class="row">
                                <div class="col-sm-6"></div>
                                <div class="col-sm-6"></div>
                            </div>
                            <div class="row">
                                <div class="col-sm-12">
                                    <table id="example2" class="table table-bordered table-hover dataTable"
                                           role="grid" aria-describedby="example2_info">
                                        <thead>
                                        <tr role="row">

                                            <th>
                                                用户名
                                            </th>
                                            <th>
                                                openid
                                            </th>
                                            <th>
                                                行走总距离
                                            </th>
                                            <th>
                                                排名
                                            </th>
                                            <th>
                                                姓名
                                            </th>
                                            <th>
                                                手机号码
                                            </th>
                                            <th>
                                                黑名单
                                            </th>
                                            <th>
                                                活动数据
                                            </th>
                                        </tr>
                                        </thead>
                                        <?php $i=1;?>
                                        <tbody>
                                        @if(count($data)>0)@foreach($data as $val)
                                            <tr role="row" class="even">

                                                <td>{{$val->nickname or ''}}</td>
                                                <td>{{$val->openid or ''}}</td>
                                                <td>{{$val->total_distance*0.1}}米</td>
                                                <td class="sorting_1">{{$i++}}</td>
                                                <td>{{$val->capital_name}}</td>
                                                <td>{{$val->capital_phone}}</td>
                                                <td>
                                                    @if($val->ishide == '0')
                                                    <a type="button" class="btn btn-primary btn-lg active btn-sm btn-dohide" data-toggle="modal" data-target="#myModal" data-uid="{{$val->userid}}" data-ishide="1">拉黑</a>
                                                    @else
                                                    <a type="button" class="btn btn-danger btn-lg active btn-sm btn-dohide" data-toggle="modal" data-target="#myModal" data-uid="{{$val->userid}}" data-ishide="0">撤回</a>
                                                    @endif
                                                
                                                </td>
                                                <td>
                                                    <a href="{{env('URL_SUB', '')}}/admin/hillclimb/rankdetail?userid={{$val->userid}}" class="btn btn-primary" target="_blank">活动详细数据</a>
                                                </td>
                                            </tr>
                                        @endforeach @endif

                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            @include("layouts.page",['paginator'=>$data])
                        </div>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </section>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">警告</h4>
            </div>
            <div class="modal-body">
                
            </div>
            <div class="modal-footer">
                <input type="hidden" name="uid" />
                <input type="hidden" name="ishide" />
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <a  class="btn btn-primary active btn-sm model_sethide" data-val="" role="button">确定</a>
            </div>
        </div>
    </div>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<script type="text/javascript">
    var url_base = "{{env('URL_SUB', '')}}";
    var total = {{$data->total()}};
    var sh;
    var shnum = 0;
    var celnum = 0;
    function _open(url){
        window.open(url+shnum,'win'+shnum);
        shnum++;
        $(".download-show").html("导出中。。。。。。");
        if(shnum >= celnum){
            $(".download-show").html("导出完毕。。。。");
            clearInterval(sh);
        }
    }
    $(function() {
        $(".btn-dohide").click(function() {
            var uid = $(this).data("uid");
            var ishide = $(this).data("ishide");
            if(ishide == '1'){
                $(".modal-body").html("你确定要拉入黑名单吗？");
            }else{
                $(".modal-body").html("你确定要撤回黑名单吗？");
            }
            $("input[name=uid]").val(uid);
            $("input[name=ishide]").val(ishide);
        });
        $(".model_sethide").click(function(){
            var uid = $('input[name=uid]').val();
            var ishide = $('input[name=ishide]').val();
            $.ajax({
                url: url_base+'/admin/hillclimb/sethide',
                type: 'POST',
                data: {uid: uid,ishide:ishide},
                dataType: 'json',
                headers: {
                    'X-CSRF-TOKEN': $('input[name=_token]').val()
                },
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                error: function() {
                    //alert('Error loading PHP document');
                },
                success: function(json) {
                    var msg = '';
                    if(ishide == '1'){
                        msg = "拉黑成功";
                    }else{
                        msg = "撤回成功";
                    }
                    var r = confirm(msg);
                    if (r == true) {
                        window.location.reload();
                    }
                }
            });
        });
        $(".btn-export").click(function(){
            $(".download-show").html("正在导出请稍后。。。。");
            celnum = 0;
            shnum = 0;
            celnum = Math.round(total/5000);
            console.log(celnum);
            var url = $(this).data("href");
            url = url + "&page=";
            sh=setInterval("_open('"+url+"')",5000); 
        });
    })
</script>
@endsection